package com.ruoyi.order.controller;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.order.config.ExportCallable;
import com.ruoyi.order.utils.zipUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/order")
public class TbOrderController extends BaseController
{
    @Autowired
    private ITbOrderService tbOrderService;

    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(TbOrder tbOrder)
    {
        startPage();
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbOrder tbOrder)
    {
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        ExcelUtil<TbOrder> util = new ExcelUtil<TbOrder>(TbOrder.class);
        util.exportExcel(response, list, "订单数据");
    }


    @Autowired
    private ExecutorService threadPool;

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(tbOrderService.selectTbOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbOrder tbOrder)
    {
        return toAjax(tbOrderService.insertTbOrder(tbOrder));
    }

    /**
     * 压缩
     * @param response
     * @param storyId
     * @throws ExecutionException
     * @throws InterruptedException
     * @throws IOException
     */

    /**
     * 压缩文档
     * @param storyId
     * @param response
     * @throws ExecutionException
     * @throws InterruptedException
     * @throws IOException
     */
    @CrossOrigin
    @GetMapping("/exportZip")
    public void exportZip(long storyId, HttpServletResponse response) throws ExecutionException, InterruptedException, IOException {
        String fileName = "D:\\JavaTest\\"  + storyId + ".xlsx";
//        List<TbOrder> list = tbOrderService.selectList();
//        EasyExcel.write(fileName, OrderVo.class).sheet("订单列表").doWrite(list);
        LocalDate date = LocalDate.now();
        String year=null;
        List<String>monthList=new ArrayList<>();
        for (int i = 1; i <=12 ; i++) {
            LocalDate localDate = date.minusMonths(i);
            LocalDate years = date.minusYears(0);
            year=years.toString().substring(0,4);
            String sj = localDate.toString().substring(0, 7);
            String[] split = sj.split("-");
            monthList.add(split[1]);
        }

        //创建任务集合
        List<FutureTask<String>> taskList=new ArrayList<>();

        for (String month : monthList) {
            //创建需要查询月份的线程
            ExportCallable exportCallable = new ExportCallable(storyId,year,month);

            FutureTask<String> task = new FutureTask<>(exportCallable);
            taskList.add(task );
            //给线程池添加任务
            threadPool.submit(task);
        }

        for (FutureTask<String> task : taskList) {
            String path = task.get();
            System.out.println(path);
        }

//        zipUtils.putZip(response,monthList, storyId);
        zipUtils.downloadZip(response,monthList, storyId,year);
        System.out.println("压缩完成!!");





    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbOrder tbOrder)
    {
        return toAjax(tbOrderService.updateTbOrder(tbOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(tbOrderService.deleteTbOrderByOrderIds(orderIds));
    }




}
